package com.sunyard.auth.controller;

import com.sunyard.auth.common.model.ResultModel;
import com.sunyard.auth.controller.convert.SmAuthUserConvert;
import com.sunyard.auth.controller.vo.SmAuthUserVO;
import com.sunyard.auth.handler.AdminHandle;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;

/**
 * @program auth
 * @description 管理员登录模块
 * @author raoinyx@163.com
 * @create 2020-07-18 00:23
 */
@RestController
@CrossOrigin(origins = "*", allowedHeaders = "*", methods = {RequestMethod.GET, RequestMethod.POST})
public class AuthLoginController {
    @Resource
    private AdminHandle adminHandle;

    @PostMapping(value = "/login", consumes = APPLICATION_JSON_VALUE)
    public ResultModel adminLogin(@RequestBody SmAuthUserVO admin) {
        String token = adminHandle.adminLogin(SmAuthUserConvert.toBO(admin));
        return new ResultModel(token);
    }

    @PostMapping("/logout")
    public ResultModel logout(HttpServletRequest request) {
        adminHandle.loginOut(request);
        return new ResultModel();
    }

}
